package cn.stard.org.controller;


import cn.stard.basic.annotation.PreAuthorize;
import cn.stard.basic.config.ExcelUtil;
import cn.stard.basic.exception.BusinessException;
import cn.stard.org.domain.ShopAuditLog;
import cn.stard.org.service.IShopService;
import cn.stard.org.domain.Shop;
import cn.stard.org.query.ShopQuery;
import cn.stard.basic.util.AjaxResult;
import cn.stard.basic.util.PageList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/shop")
public class ShopController {
    @Autowired
    public IShopService shopService;


    /**
     * 保存和修改公用的
     * @param shop  传递的实体
     * @return Ajaxresult转换结果
     */
    @PutMapping
    @PreAuthorize(name="店铺添加", sn = "shop:addOrUpdate")
    public AjaxResult addOrUpdate(@RequestBody Shop shop){
        try {
            if( shop.getId()!=null)
                shopService.update(shop);
            else
                shopService.save(shop);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("保存对象失败！"+e.getMessage());
        }
    }
    /**
     * 删除对象信息
     * @param id
     * @return
     */
    @DeleteMapping(value="/{id}")
    @PreAuthorize(name="店铺删除", sn = "shop:delete")
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            shopService.remove(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("删除对象失败！"+e.getMessage());
        }
    }

    //获取用户
    @GetMapping("/{id}")
    @PreAuthorize(name="通过id获取店铺", sn = "shop:get")
    public Shop get(@PathVariable("id")Long id)
    {
        return shopService.loadById(id);
    }


    /**
     * 查看所有的员工信息
     * @return
     */
    @GetMapping()
    @PreAuthorize(name="获取所有店铺", sn = "shop:list")
    public List<Shop> list(){

        return shopService.loadAll();
    }


    /**
     * 分页查询数据
     *
     * @param query 查询对象
     * @return PageList 分页对象
     */
    @PostMapping("/list")
    @PreAuthorize(name="分页获取所有店铺", sn = "shop:list")
    public PageList<Shop> json(@RequestBody ShopQuery query)
    {
        return
                shopService.queryData(query);
    }

    /**
     * @Title: settlement
     * @Description: 店铺入驻接口
     * @Author: Mr.She
     * @Version: 1.0
     * @Date:  2022/7/1 14:08
     * @Parameters: [department]
     * @Return cn.stard.basic.utils.AjaxResult
     */
    @PostMapping("/settlement")
    @PreAuthorize(name="店铺入驻", sn = "shop:settlement")
    public AjaxResult settlement(@RequestBody Shop shop){
        try {
            shopService.settlement(shop);
            return AjaxResult.me();
        } catch (BusinessException e){
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙，请重试！");
        }
    }


    /*通过接口来调用方法设置处理驳回店铺信息*/
    @PostMapping("/audit/reject")
    @PreAuthorize(name="店铺驳回", sn = "shop:reject")
    public AjaxResult reject(@RequestBody ShopAuditLog shopAuditLog ){
        try {
            shopService.reject(shopAuditLog );
            return AjaxResult.me();
        } catch (BusinessException e){
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙，请重试！");
        }
    }

    @PostMapping("/audit/pass")
    @PreAuthorize(name="店铺通过", sn = "shop:pass")
    public AjaxResult pass(@RequestBody ShopAuditLog shopAuditLog,Shop shop ){
        try {
            shopService.pass(shopAuditLog );
            return AjaxResult.me();
        } catch (BusinessException e){
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙，请重试！");
        }
    }

    @PostMapping("/import/excel")
    @PreAuthorize(name="店铺导入文件", sn = "shop:importExcel")
    public void importExcel(@RequestPart(value = "file", required = true)MultipartFile file){
        // 1.使用EasyPoi帮你解析Excel，解析成Java对象
        // MultipartFile file, Integer titleRows, Integer headerRows, Class<T> pojoClass
        List<Shop> shops = ExcelUtil.importExcel(file, 0, 1, Shop.class);
        // 2.做批量添加，自行实现
        shops.forEach(System.out::println);
    }

    @GetMapping("/export/excel")
    @PreAuthorize(name="店铺导出文件", sn = "shop:importExcel")
    public void export(HttpServletResponse resp){

        List<Shop> shops = shopService.loadAll();
        ExcelUtil.exportExcel(shops,null,"店铺信息",Shop.class,"店铺信息",resp);
    }

    @GetMapping("/echarts")
//    @PreAuthorize(name="店铺首页", sn = "shop:importExcel")
    public Map<String, Object> echarts(){
        return shopService.loadEcharts();
    }





    @PostMapping("/audit{id}")
    @PreAuthorize(name="店铺id查询", sn = "shop:audit")
    public AjaxResult audit(@PathVariable("id") Shop shop){
        try {
            shopService.settlement(shop);
            return AjaxResult.me();
        } catch (BusinessException e){
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙，请重试！");
        }
    }

}


